/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxContentHolder
 * @Package com.rx.core.util
 * @Description: 上下文
 * @author: 陈锦韬
 * @date: 2021\6\25 0025
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.util;

import com.rx.core.base.RxBaseContent;
import com.rx.core.bean.LoginUserInfo;
import com.rx.core.enm.RX_USER_TYPE;
import com.rx.core.iface.RxContent;
import org.springframework.boot.actuate.endpoint.SecurityContext;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author: Administrator
 * @Description: 上下文
 * @date: 2021\6\25 0025
 */
public class RxContentHolder {
    private static final ThreadLocal<RxContent> contextHolder = new InheritableThreadLocal<>();

    public static void clearContext() {
        contextHolder.remove();
    }

    public static RxContent getContext() {
        RxContent ctx = contextHolder.get();

        if (ctx == null) {
            ctx = createEmptyContext();
            contextHolder.set(ctx);
        }

        return ctx;
    }
    public void setContext(RxContent context) {
        Assert.notNull(context, "Only non-null SecurityContext instances are permitted");
        contextHolder.set(context);
    }

    public static RxContent createEmptyContext() {
        return new RxBaseContent();
    }

    public static LoginUserInfo getUser(){
        return getContext().getUserInfo();
    }
    public static LoginUserInfo.UserInfo getUserInfo(){
        return getContext().getUserInfo().getUserInfo();
    }
    public static List<LoginUserInfo.OrgInfo> getOrgInfo() {
        return getContext().getUserInfo().getOrgList();
    }
    public static List<LoginUserInfo.OrgInfo> getTopOrgInfo() {
        return getContext().getUserInfo().getTopOrgList();
    }
    public static LoginUserInfo.OrgInfo getCurrentOrg() {
        return getContext().getUserInfo().getCurrentOrg();
    }
    public static LoginUserInfo.OrgInfo getCurrentOrg(String bid) {
        if(EmptyChecker.isEmpty(bid)){
            return getCurrentOrg();
        }
        if(EmptyChecker.isEmpty(getContext().getUserInfo().getTopOrgList())){
            return getCurrentOrg();
        }
        for (LoginUserInfo.OrgInfo orgInfo : getContext().getUserInfo().getTopOrgList()) {
            if(bid.equalsIgnoreCase(orgInfo.getBid())){
                RxContentHolder.getContext().getUserInfo().setCurrentOrg(orgInfo);
                return orgInfo;
            }
        }
        return getCurrentOrg();
    }
    public static List<LoginUserInfo.RoleInfo> getRoleInfo() {
        return getContext().getUserInfo().getRoleList();
    }

    public static boolean hasRole(String role){
        List<LoginUserInfo.RoleInfo> roleInfoList = RxContentHolder.getRoleInfo();
        for (LoginUserInfo.RoleInfo roleInfo : roleInfoList) {
            if (roleInfo.getRoleCode().equalsIgnoreCase(role)){
                return true;
            }
        }
        return false;
    }
    public static boolean systemUser(){
        String userType = RxContentHolder.getUserInfo().getUserType();
        return RX_USER_TYPE.SYS.getCode().equalsIgnoreCase(userType);
    }

}
